package team;
import javax.swing.*;

import autoproject.SpringUtilities;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * Frame to Add new Auto
 * @author jerry
 *
 */

public class AddNewAutoFrame extends JFrame{
	//instance variables
	private final JFrame frame = this;
	
	public AddNewAutoFrame(final JFrame autoFrame, final boolean authorized) {
		final JTextField[] inputArray = new JTextField[7];
		
		//Store the new car Information
		final String[] newAutoInfo = new String[8];
		
		setLayout(new GridLayout(2, 1));
		
		//set up the text field panel
		String[] options = {"AutoMaker: ", "Model: ", "Year: ", "Description: ", "Liter: ", "Engine: ", "Cubic"};
		JPanel textPanel = new JPanel(new SpringLayout());
		for (int i = 0; i < options.length; i++) {
			JLabel l = new JLabel(options[i], JLabel.TRAILING);
			textPanel.add(l);
			JTextField textField = new JTextField(10);
			inputArray[i] = textField;
			l.setLabelFor(textField);
			textPanel.add(textField);
		}
		SpringUtilities.makeCompactGrid(textPanel, options.length, 2, 6, 6, 6, 6);
		
		//button panel
		JPanel btnPanel = new JPanel(new FlowLayout());
		final JButton updateBtn = new JButton("Submit New Car");
		final JButton addNewParts = new JButton("Add New Parts to Car");
		final JButton addExistingParts = new JButton("Add Existing Parts to Car");
		final JButton backBtn = new JButton("Back");
		
		addNewParts.setEnabled(false);
		addExistingParts.setEnabled(false);
		
		btnPanel.add(updateBtn);
		//btnPanel.add(addNewParts);
		btnPanel.add(addExistingParts);
		btnPanel.add(backBtn);
		
		//add button actions
		updateBtn.addActionListener(new ActionListener() {
			@Override 
			public void actionPerformed(ActionEvent e) {
				String[] updatedInfo = new String[8];
				for (int i = 0; i < inputArray.length; i++) {
					System.out.print(inputArray[i].getText() + " ");
					updatedInfo[i] = inputArray[i].getText().toUpperCase();
				}
				System.out.println();
				if (AutoMaker.makerTable.containsKey(updatedInfo[0]))
					updatedInfo[0] = AutoMaker.makerTable.get(updatedInfo[0]);
				else {
					AutoMaker.addMaker(updatedInfo[0], "OTR" + updatedInfo[0]);
					updatedInfo[0] = AutoMaker.makerTable.get(updatedInfo[0]);
				}
				updatedInfo[7] = Integer.toString(Result.getMaxRLink() + 1);
				boolean added = Result.addNewRLink(updatedInfo[7]);
				added = Result.createNewAuto(updatedInfo);
				if (added) {		
					for (int i = 0; i < newAutoInfo.length; i++) {
						newAutoInfo[i] = updatedInfo[i];
					}
					addNewParts.setEnabled(true);
					addExistingParts.setEnabled(true);
					updateBtn.setEnabled(false);
					System.out.println("Add New Auto Successful");
				}
			}
		});
		
		addNewParts.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Integer rLink = Integer.parseInt(newAutoInfo[7]);
				AddNewPartFrame addPartFrame = new AddNewPartFrame(frame, rLink);
				frame.dispose();
			}
		});
		
		addExistingParts.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				Integer rLink = Integer.parseInt(newAutoInfo[7]);
				PartFrame partFrame = new PartFrame(frame, rLink, null);
				frame.dispose();
			}
			
		});
		
		backBtn.addActionListener(new ActionListener() {
			@Override 
			public void actionPerformed(ActionEvent e) {
				AutoMakerFrame newMakerFrame = new AutoMakerFrame(new InitialFrame(), authorized);
				frame.dispose();
			}
		});
		
		this.setTitle("Edit Auto");
		this.setSize(800,600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		textPanel.setOpaque(true);
//		add(textPanel, BorderLayout.CENTER);
		add(textPanel);
		textPanel.setBorder(BorderFactory.createTitledBorder("Add New Car: "));
//		add(btnPanel, BorderLayout.SOUTH);
		add(btnPanel);
		
//		this.pack();
		this.setVisible(true);
	}

}
